package com.johnny.action;

import java.net.Socket;
import java.util.List;

import com.johnny.data.DataPool;
import com.johnny.data.PlayerInfo;
import com.johnny.data.RoomInfo;
import com.johnny.data.TableInfo;
import com.johnny.startup.Casino28App;

import net.sf.json.JSONObject;

public class SitAction implements Action {

	@Override
	public String doAction(JSONObject object, Socket socket) {
		JSONObject result = new JSONObject();
		result.put("action", object.getString("action"));
		List<RoomInfo> rooms=DataPool.getInstance().getRoomInfos();
		RoomInfo room = null;
		TableInfo table = null;
		for (RoomInfo roomInfo : rooms) {
			if (object.getString("roomId").equals(roomInfo.getRoomId())) {
				room = roomInfo;
				for (TableInfo tableInfo : room.getTables()) {
					if (tableInfo.getTableId().equals(object.getString("tableId"))) {
						table = tableInfo;
					}
				}
				break;
			}
		}
		PlayerInfo player = Casino28App.connector.getPlayerInfoBySocket(socket);
		for (RoomInfo roomInfo :rooms) {
			for (TableInfo tableInfo : roomInfo.getTables()) {
				if (tableInfo.getBanker().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
				if (tableInfo.getPlayer1().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
				if (tableInfo.getPlayer2().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
				if (tableInfo.getPlayer3().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
				if (tableInfo.getPlayer4().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
				if (tableInfo.getPlayer5().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
				if (tableInfo.getPlayer6().equals(player.getName())) {
					result.put("state", 1);
					return result.toString();
				}
			}
		}

		String position = object.getString("pos");
		switch (position) {
		case "player1":
			if (table.getPlayer1().isEmpty()) {
				result.put("state", 0);
				table.setPlayer1(player.getName());

				JSONObject object2 = new JSONObject();
				object2.put("action", "BcSit");
				object2.put("room", room.toJsonObject());
				Casino28App.connector.broadcast(object2.toString(), room.getRoomId());
			} else {
				result.put("state", 1);
			}
			break;
		case "player2":
			if (table.getPlayer2().isEmpty()) {
				result.put("state", 0);
				table.setPlayer2(player.getName());

				JSONObject object2 = new JSONObject();
				object2.put("action", "BcSit");
				object2.put("room", room.toJsonObject());
				Casino28App.connector.broadcast(object2.toString(), room.getRoomId());
			} else {
				result.put("state", 1);
			}
			break;
		case "player3":
			if (table.getPlayer3().isEmpty()) {
				result.put("state", 0);
				table.setPlayer3(player.getName());

				JSONObject object2 = new JSONObject();
				object2.put("action", "BcSit");
				object2.put("room", room.toJsonObject());
				Casino28App.connector.broadcast(object2.toString(), room.getRoomId());
			} else {
				result.put("state", 1);
			}
			break;
		case "player4":
			if (table.getPlayer4().isEmpty()) {
				result.put("state", 0);
				table.setPlayer4(player.getName());

				JSONObject object2 = new JSONObject();
				object2.put("action", "BcSit");
				object2.put("room", room.toJsonObject());
				Casino28App.connector.broadcast(object2.toString(), room.getRoomId());
			} else {
				result.put("state", 1);
			}
			break;
		case "player5":
			if (table.getPlayer5().isEmpty()) {
				result.put("state", 0);
				table.setPlayer5(player.getName());

				JSONObject object2 = new JSONObject();
				object2.put("action", "BcSit");
				object2.put("room", room.toJsonObject());
				Casino28App.connector.broadcast(object2.toString(), room.getRoomId());
			} else {
				result.put("state", 1);
			}
			break;
		case "player6":
			if (table.getPlayer6().isEmpty()) {
				result.put("state", 0);
				table.setPlayer6(player.getName());

				JSONObject object2 = new JSONObject();
				object2.put("action", "BcSit");
				object2.put("room", room.toJsonObject());
				Casino28App.connector.broadcast(object2.toString(), room.getRoomId());
			} else {
				result.put("state", 1);
			}
			break;
		default:
			break;
		}
		return result.toString();
	}
}
